﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Web;
using System.Globalization;

namespace University.Workshop.Business
{
    public static class UserUpdateManager
    {
        public static User UpdateUser(String phone, String mobilePhone, String city, String department, String email)
        {
            if (string.IsNullOrEmpty(city) || string.IsNullOrEmpty(department) || string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(mobilePhone))
            {
                throw new ValidationException("All fields are required");
            }
            
            string numberPattern = "\\d+";
            if (!Regex.IsMatch(phone, numberPattern))
            {
                throw new ValidationException("Invalid phone");
            }
            
            if (!Regex.IsMatch(mobilePhone, numberPattern))
            {
                throw new ValidationException("Invalid MobilePhone");
            }

            string stringPattern = @"^[a-zA-Z]+$";
            if (!Regex.IsMatch(city, stringPattern))
            {
                throw new ValidationException("Invalid city");
            }

            User user = new User();
            user.ContactInformation.Phone = Convert.ToInt32(phone, CultureInfo.CurrentCulture);
            user.ContactInformation.MobilePhone = Convert.ToInt32(mobilePhone, CultureInfo.CurrentCulture);
            user.ContactInformation.City = city;
            user.ContactInformation.Department = department;
            user.ContactInformation.Email = email;
            
            UserDataAccess.UpdateUserInformation(user);
            return user;
        }
    }
}
